package com.base.component.app.push.jpush;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;

import com.base.component.app.push.bo.PushBO;
import com.base.component.app.push.bo.PushIdBO;
import com.base.component.app.push.bo.PushListBO;
import com.base.component.app.push.core.PushHandler;
import com.gitee.magic.framework.base.constant.Config;

import cn.jiguang.sdk.api.PushApi;
import cn.jiguang.sdk.bean.push.PushSendParam;
import cn.jiguang.sdk.bean.push.PushSendResult;
import cn.jiguang.sdk.bean.push.audience.Audience;
import cn.jiguang.sdk.bean.push.message.notification.NotificationMessage;
import cn.jiguang.sdk.bean.push.message.notification.NotificationMessage.Android.Intent;
import cn.jiguang.sdk.bean.push.options.Options;
import cn.jiguang.sdk.enums.platform.Platform;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author start
 */
@Slf4j
public abstract class JpushBase<P extends PushBO> implements PushHandler<P> {
	
	@Value("${packageName.android}")
	private String androidPnPackageName;
	
	@Value("${push.jpush.appKey}")
	private String appKey;
	@Value("${push.jpush.masterSecret}")
	private String masterSecret;
	
	public void push(PushIdBO o) {
		PushListBO p=new PushListBO();
		BeanUtils.copyProperties(o, p);
		p.setTokenIds(Arrays.asList(o.getTokenId()));
		push(p);
	}
	
	public void push(PushListBO o) {
        Audience audience = new Audience();
        audience.setRegistrationIdList(o.getTokenIds());
		push(audience,o);
	}
	
	private void push(Audience audience,PushBO o) {
		Map<String,Object> extras=new HashMap<>(1);
		extras.put("msg", o.getParams());
		
		StringBuilder sb=new StringBuilder();
		for(String s:o.getParams().keySet()) {
			Object v=o.getParams().get(s);
			if(v.getClass().equals(Integer.class)) {
				sb.append("i.");
			}else {
				sb.append("S.");
			}
			sb.append(s+"="+v+";");
		}
		
		PushApi pushApi=new PushApi.Builder().setAppKey(appKey).setMasterSecret(masterSecret).build();
		
		PushSendParam param = new PushSendParam();
		//应用外
		NotificationMessage.Android android = new NotificationMessage.Android();
		android.setAlert(o.getBody());
		android.setTitle(o.getTitle());
		android.setExtras(extras);
		Intent intent=new Intent();
		intent.setUrl("intent:#Intent;action=communication.dn.an.splash;component="+androidPnPackageName+"/com.genius.withyou.splash.SplashActivity;"+sb.toString()+"end");
		android.setIntent(intent);
		
		NotificationMessage.IOS ios = new NotificationMessage.IOS();
		ios.setAlert(o.getBody());
		ios.setExtras(extras);
		
		NotificationMessage.HMOS hmos=new NotificationMessage.HMOS();
		hmos.setAlert(o.getBody());
		hmos.setTitle(o.getTitle());
		hmos.setCategory("IM");
		hmos.setExtras(extras);
		
		NotificationMessage notificationMessage = new NotificationMessage();
		notificationMessage.setAlert(o.getBody());
		notificationMessage.setAndroid(android);
		notificationMessage.setIos(ios);
		notificationMessage.setHmos(hmos);
		
		param.setNotification(notificationMessage);
		param.setAudience(audience);
		param.setPlatform(Arrays.asList(Platform.android,Platform.ios));
		Options options=new Options();
		if(!Config.isSystemFlag()) {
			//IOS apns需要指定对应的环境
			options.setApnsProduction(false);
		}
		
		Map<String,Object> thirdPartyChannel=new HashMap<>(3);
		Map<String,Object> huaweiOptions=new HashMap<>(3);
		huaweiOptions.put("category", "IM");
		huaweiOptions.put("importance", "NORMAL");
		thirdPartyChannel.put("huawei", huaweiOptions);
		options.setThirdPartyChannel(thirdPartyChannel);
		param.setOptions(options);
		
        PushSendResult result=pushApi.send(param);
        log.info("jpush result:{}",result);
	}
	
}
